package com.cienet.playandrecord;

import java.io.ByteArrayInputStream;
import java.io.IOException;

import javax.microedition.media.Manager;
import javax.microedition.media.MediaException;
import javax.microedition.media.Player;
import javax.microedition.media.PlayerListener;

import mypackage.EchoScreen;

public class PlaySide
{
    
    private static final int PLAYER_COUNT = 2;
    
    private int mPlayIndex = 0;
    
    //    private ByteArrayInputStream[] mByteArrayInputStreams;
    //    
    //    private Player[] mPlayers;
    
    private ByteArrayInputStream mByteArrayInputStream;
    
    private Player mPlayer;
    
    public PlaySide()
    {
        //        prePare();
    }
    
    private void nextIndex()
    {
        mPlayIndex++;
        if (mPlayIndex >= PLAYER_COUNT)
        {
            mPlayIndex = 0;
        }
    }
    
    private void prePare()
    {
        EchoScreen.log("prePare mPlayIndex:" + mPlayIndex);
        try
        {
            mByteArrayInputStream = new ByteArrayInputStream(
                    BufferManaer.getPlayBuffer());
            mPlayer = Manager.createPlayer(mByteArrayInputStream, "audio/amr");
            
            mPlayer.addPlayerListener(mPlayerListener);
            //            mPlayer.prefetch();
            EchoScreen.log("prePare end");
            
        }
        catch (IOException e)
        {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        catch (MediaException e)
        {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }
    
    public void beginPlay()
    {
        try
        {
            prePare();
            
            EchoScreen.log("start Play");
            //            mPlayer = mPlayers[mPlayIndex];
            mPlayer.start();
            EchoScreen.log("Playing");
            //            nextIndex();
        }
        catch (MediaException e)
        {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }
    
    public void stop()
    {
        EchoScreen.log("play stop!!!!");
        for (int i = 1; i < PLAYER_COUNT; i++)
        {
            Player player = mPlayer;
            if (player == null)
                return;// nothing to stop
            try
            {
                if (player.getState() == Player.STARTED)
                {
                    player.stop();
                }
                if (player.getState() != Player.CLOSED)
                {
                    player.close();
                }
            }
            catch (MediaException e)
            {
                EchoScreen.log("RecvStream media stop exception:"
                        + e.toString());
            }
        }
    }
    
    // test
    PlayerListener mPlayerListener = new PlayerListener()
    {
        // get the event from player.
        public void playerUpdate(Player arg0, String event, Object eventData)
        {
            if (event == PlayerListener.END_OF_MEDIA)
            {
                EchoScreen.log("Player event: END_OF_MEDIA");
                try
                {
                    if (mPlayer.getState() == Player.STARTED)
                    {
                        mPlayer.stop();
                    }
                    if (mPlayer.getState() != Player.CLOSED)
                    {
                        mPlayer.close();
                    }
                    //                    mPlayer.removePlayerListener(mPlayerListener);
                    
                    beginPlay();
                }
                catch (MediaException e)
                {
                    EchoScreen.log("RecvStream media stop exception:"
                            + e.toString());
                }
            }
        }
    };
}
